package com.futures.questionbank.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;

import androidx.viewpager.widget.ViewPager;

import com.futures.questionbank.R;
import com.futures.questionbank.db.Constants;
import com.futures.questionbank.db.table.option;

import java.util.List;


public class OptionsAdpter extends BaseRecycleAdapter<option> {
    public int pos = 500;

    private List<option> dataList;
    private Context mContext;
    private BaseViewHolder.onItemCommonClickListener commonClickListener;
    private RadioButton rb,rb_select;
    private String type,my_answer;
    private int i;
    public ViewPager viewPager;
   // public HashMap<Integer, String> maps = new HashMap<>();

    public void setCheck(int i){
        this.pos = i;
        notifyDataSetChanged();
    }



    public OptionsAdpter(Context context, String my_answer, String type, List<option> dataList, int position, ViewPager viewPager, BaseViewHolder.onItemCommonClickListener commonClickListener) {
        super(context, dataList, R.layout.itme_option_list);
        this.dataList = dataList;
        this.commonClickListener = commonClickListener;
        mContext = context;
        this.type = type;
        this.viewPager = viewPager;
        this.i = position;
        this.my_answer = my_answer;
    }

    @Override
    public void bindData(BaseViewHolder holder, option data) {
        //  holder.setText(R.id.tv_title,data.getAnswer());
        holder.setText(R.id.tv_title, data.getAnswer());
        if (holder.getAdapterPosition()==0){
            holder.setText(R.id.rb,"A");
        }else if (holder.getAdapterPosition()==1){
            holder.setText(R.id.rb,"B");
        }else if (holder.getAdapterPosition()==2){
            holder.setText(R.id.rb,"C");
        }else {holder.setText(R.id.rb,"D");}

        RadioButton  rb = holder.getView(R.id.rb);
        if (!TextUtils.isEmpty(my_answer)){
            try {
                int a = Integer.parseInt(my_answer);
                if (holder.getAdapterPosition() == a){
                    rb.setChecked(true);
                }
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        holder.getView(R.id.ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Constants.maps.put(i+1,holder.getAdapterPosition() + "");
               //  rl.setBackgroundResource(R.drawable.shape_blue);
                 //  notifyDataSetChanged();
              //  rb.setChecked(true);
                if (type.contains("多选")){
                    rb.setChecked(true);
                }else {
                   if (rb_select == rb) {
                        rb.setChecked(false);
                        rb_select = null;
                      // viewPager.setCurrentItem(holder.getAdapterPosition() + 1);
                        return;
                    }

                    rb.setChecked(true);
                   /* if (holder.getAdapterPosition() == 0){
                        Constants.maps.put(i+1,"A");
                    }else if (holder.getAdapterPosition() == 1){
                        Constants.maps.put(i+1,"B");
                    }else if (holder.getAdapterPosition() == 2){
                        Constants.maps.put(i+1,"C");
                    }else {
                        Constants.maps.put(i+1,"D");
                    }*/

                    if (rb_select != null) {
                        rb_select.setChecked(false);
                    }
                    rb_select = rb;
                  //  viewPager.setCurrentItem(holder.getAdapterPosition() + 1);
                }
            }
        });
    }
}
